
// -----------------------------------------------------------------------------

#include "stdafx.h"
#include "Win32.Xml.XmlWriter.h"
#include "Win32.IO.File.h"
#include "Win32.IO.TextFile.h"

namespace XML = Kif::Win32::Xml;

namespace Kif
{
namespace Win32
{
namespace Xml
{

// -----------------------------------------------------------------------------

namespace XmlDocumentElements
{
   TCHAR xmlVersion[] = _T("<?xml version=\"1.0\"?>");
}  // end of XmlDocumentElements

namespace XmlDocumentTags
{
   //extern TCHAR xmlVersion[];
}  // end of XmlDocumentTags

// -----------------------------------------------------------------------------

/// <summary>Xml Document (ctor)</summary>
void XmlDocument::InitDocument(
   TCHAR *tag, DWORD tagLength,
   Kif::Win32::String *attributes, int attributeCount)
{
   linkList.Add(tag, tagLength);

   XmlText.Copy(XmlDocumentElements::xmlVersion,
      SizeOf(XmlDocumentElements::xmlVersion));
   XML::XmlWriter::WriteElement(
      tag, tagLength, attributes, attributeCount, XmlText);
}  // end of InitDocument (ctor)

/// <summary>Write start tag and push Tag to stack.</summary>
/// <param name="tag">tag</param>
/// <param name="tagLength">tag string length</param>
void XmlDocument::WriteStartTag(TCHAR *tag, size_t tagLength,
   TCHAR *tagAttributes, size_t tagAttributesLength)
{
   if (tag == NULL)
      return;
   XML::XmlWriter::WriteStartTag(tag, tagLength,
      tagAttributes, tagAttributesLength, &XmlText);
   linkList.Add(tag, tagLength);
}  // end of WriteStartTag

/// <summary>Write start tag and push Tag to stack.</summary>
/// <param name="tag">tag</param>
/// <param name="tagLength">tag string length</param>
void XmlDocument::WriteStartTag(TCHAR *tag, size_t tagLength)
{
   if (tag == NULL)
      return;
   XML::XmlWriter::WriteStartTag(tag, tagLength, XmlText);
   linkList.Add(tag, tagLength);
}  // end of WriteStartTag

/// <summary>Write end tag and pop Tag from stack.</summary>
void XmlDocument::WriteEndTag()
{
   Kif::Win32::String *tag = linkList.Peek();
   if (tag == NULL)
      return;
   XML::XmlWriter::WriteEndTag(tag->GetBuffer(), tag->GetLength(), XmlText);
   linkList.Pop();
}  // end of WriteEndTag

/// <summary>Write XML text to a file using given output file-path.</summary>
/// <param name="outFilePath">Output file path</param>
/// <returns>true is returned if all is ok</returns>
bool XmlDocument::ToFile(TCHAR *outFilePath)
{
   bool done = false;

   // if file exists it is deleted.
   Kif::Win32::IO::File file;
   if (file.Exists(outFilePath))
   {
      done = file.Delete(outFilePath);
      if (!done)
      {
         // TODO: log error message...
         return done;
      }
   }

   // try to write XML text to a file using given file path...
   Kif::Win32::IO::TextFile outFile;
   done = outFile.OpenReadWrite(outFilePath);
   if (done)
   {
      done = outFile.Write(XmlText) > 0;
      outFile.Close();
   }

   return done;
}  // end of ToFile

}  // end of Kif::Win32::Xml namespace
}  // end of Kif::Win32 namespace
}  // end of Kif namespace

// -----------------------------------------------------------------------------

